import Vue from 'vue'
import Router from 'vue-router'
import Home from '@/views/Home'
import Login from '@/views/Login'
import Register from '@/views/Register'
import ProjectDetail from '@/views/ProjectDetail'
import CreateProject from '@/views/CreateProject'
import EditProject from '@/views/EditProject'
import HistoryList from '@/views/HistoryList'
import Statistics from '@/views/Statistics'

Vue.use(Router)

const router = new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  routes: [
    {
      path: '/',
      name: 'home',
      component: Home,
      meta: { title: '首页 - 医疗数据众包平台' }
    },
    {
      path: '/login',
      name: 'login',
      component: Login,
      meta: { title: '登录 - 医疗数据众包平台' }
    },
    {
      path: '/register',
      name: 'register',
      component: Register,
      meta: { title: '注册 - 医疗数据众包平台' }
    },
    {
      path: '/dashboard',
      name: 'dashboard',
      component: () => import('../views/Dashboard.vue'),
      meta: { requiresAuth: true },
      beforeEnter: (to, from, next) => {
        const store = router.app.$store;
        if (store.getters['auth/isResearcher']) {
          next('/researcher-projects');
        } else {
          next('/participant-projects');
        }
      }
    },
    {
      path: '/project/:id',
      name: 'project-detail',
      component: ProjectDetail,
      props: true,
      meta: { requiresAuth: false, title: '项目详情 - 医疗数据众包平台' }
    },
    {
      path: '/create-project',
      name: 'create-project',
      component: CreateProject,
      meta: { 
        requiresAuth: true, 
        requiresResearcher: true, 
        title: '创建项目 - 医疗数据众包平台'
      },
      beforeEnter(to, from, next) {
        const store = router.app.$store;
        const isLoggedIn = store.getters['auth/isAuthenticated'];
        const isResearcher = store.getters['auth/isResearcher'];
        
        console.log('正在导航到创建项目页面，用户状态:', {
          path: to.path,
          isLoggedIn,
          isResearcher
        });
        
        if (!isLoggedIn) {
          console.log('用户未登录，重定向到登录页');
          next('/login');
        } else if (!isResearcher) {
          console.log('用户不是研究者，无法访问创建项目页面');
          next('/');
        } else {
          console.log('用户是研究者，允许访问创建项目页面');
          next();
        }
      }
    },
    {
      path: '/edit-project/:id',
      name: 'edit-project',
      component: EditProject,
      props: true,
      meta: { requiresAuth: true, requiresResearcher: true, title: '编辑项目 - 医疗数据众包平台' }
    },
    {
      path: '/history',
      name: 'history',
      component: HistoryList,
      meta: { requiresAuth: true, title: '历史记录 - 医疗数据众包平台' }
    },
    {
      path: '/statistics',
      name: 'statistics',
      component: Statistics,
      meta: { requiresAuth: true, title: '项目统计 - 医疗数据众包平台' }
    },
    {
      path: '/researcher-projects',
      name: 'researcher-projects',
      component: () => import('../views/ResearcherProjects.vue'),
      meta: { requiresAuth: true, requiresResearcher: true, title: '我的项目 - 医疗数据众包平台' }
    },
    {
      path: '/participant-projects',
      name: 'participant-projects',
      component: () => import('../views/ParticipantProjects.vue'),
      meta: { requiresAuth: true, requiresParticipant: true, title: '项目参与 - 医疗数据众包平台' }
    }
  ]
})

// 更新页面标题
router.afterEach((to) => {
  document.title = to.meta.title || '医疗数据众包平台'
})

router.beforeEach((to, from, next) => {
  const store = router.app.$store;
  
  // 使用store的getters获取认证状态和角色
  const isLoggedIn = store.getters['auth/isAuthenticated'];
  const isResearcher = store.getters['auth/isResearcher'];
  
  // 打印调试信息
  console.log('路由守卫状态检查:', { 
    path: to.path,
    isLoggedIn, 
    isResearcher,
    requiresAuth: to.matched.some(record => record.meta.requiresAuth),
    requiresResearcher: to.matched.some(record => record.meta.requiresResearcher)
  });
  
  if (to.matched.some(record => record.meta.requiresAuth)) {
    if (!isLoggedIn) {
      console.log('未登录，重定向到登录页');
      next({ name: 'login' });
    } else if (to.matched.some(record => record.meta.requiresResearcher) && !isResearcher) {
      console.log('用户不是研究者，无法访问研究者页面');
      next({ name: 'home' });
    } else {
      console.log('用户已授权，允许访问');
      next();
    }
  } else {
    next();
  }
})

export default router 